package com.hupu.jrs.ui.report

import com.hupu.jrs.injector.PerActivity
import com.hupu.jrs.util.ToastUtil
import javax.inject.Inject
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Action1

/**
 * Created by sll on 2016/3/11.
 */
@PerActivity
class ReportPresenter @Inject
constructor(private val mForumApi: ForumApi) : ReportContract.Presenter {

    private var mSubscription: Subscription? = null
    private var mReportView: ReportContract.View? = null

    override fun submitReports(tid: String, pid: String, type: String, content: String) {
        mReportView!!.showLoading()
        mSubscription = mForumApi.submitReports(tid, pid, type, content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Action1<Any> { result ->
                    mReportView!!.hideLoading()
                    if (result.status === 200) {
                        mReportView!!.reportSuccess()
                        ToastUtil.showToast("举报成功~")
                    } else {
                        ToastUtil.showToast("举报失败，请检查网络后重试")
                    }
                }, Action1<Throwable> {
                    mReportView!!.hideLoading()
                    ToastUtil.showToast("举报失败，请检查网络后重试")
                })
    }

    override fun attachView(view: ReportContract.View) {
        mReportView = view
    }

    override fun detachView() {
        if (mSubscription != null && !mSubscription!!.isUnsubscribed) {
            mSubscription!!.unsubscribe()
        }
        mReportView = null
    }
}
